package com.xfgg.cloud.domain.service;

import com.xfgg.cloud.domain.model.aggregate.User;
import com.xfgg.cloud.domain.model.entities.JwtUser;
import com.xfgg.cloud.domain.model.entities.UserInfo;
import com.xfgg.cloud.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 用户领域服务
 *
 * @author xfgg
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserService {
    private final UserRepository userRepository;

    public User getUserByName(String userName, String passWord) {
        UserInfo userInfo = userRepository.getUserByUserName(userName);
        Optional.ofNullable(userInfo).orElseThrow(() -> new RuntimeException("用户名不存在"));
        if (!userInfo.getPassWord().equals(passWord)) {
            throw new RuntimeException("密码错误");
        }
        User user = new User();
        user.setUid(userInfo.getId());
        user.setUserInfo(userInfo);
        user.setJwtUser(new JwtUser(userInfo));
        return user;
    }
}
